package com.study.epay.config;

import lombok.Data;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/10/17 13:34
 */
@Data
//@Slf4j
@Component
public class AliPayProperties
{
    /*根路径*/
    public String root_path = "E:\\JavaSpace\\chapterspace\\Character4\\AliPay\\src\\main\\resources\\static\\crt\\";

    private String APP_ID = "2016102400751563";
    private String PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCiTDXxwTrzn/vR7j/bXMFb2fv6iSjZ4NbellyUNCDtd6uj7P6VRLG8x9C7vEhgdFu7jR75cSNJVribh+VEQXVaSHQHExq9a88nvqBopMmz261xTFs7Ukd9aC6zMtbYZ7SFiK0fF/zbD1G4m4qzgXf8s4HfKOHz8pgdPSIpldU7rGPLhqiNYynBuhg/9v7iOz6SzfxNc6saA6Z9B/W1xrDGe7BUxdDoetZ3T+Mf0K/LmWWfsgPuXcac7NwtAChDqsYKK90nBN0XiAioaH7E+c+jvWWDfYzCUOWfNUgz+Hj65PVOieE4bPDsKqp6/8VDrsm2YiE5ukMOtxP5a8MKYop7AgMBAAECggEAe3hDKdJ5SNKTP1hjmDzulTQ28JjcTTUyAlCdydoGmuIJ4EHyRBjoptfUwp4w6cBb8UkhIV+jGq4w75HpJ44s+yxXqPIty6FzDaeGXVXQZVZgkusmFPE5lfxmWsn4jNYIjfY2zorvQLBc2XbrhDig0cy2ptFxZKdQdh4GGWD1wdjkMMyo3tDz7abhkG8Togn2z5LxEeGKyirZkC65njrX/AibUItRJGOCIBYHGuQBbPErSSfP6UZ9PkHT/BQe5yIEvY6EVTr8lq5fGK9hBAnSusz72IkVEXRKK18MIj6D6e/rfxnBsPqjxl9yqvxUCMpMIaU9+WgWJs7PGFibQl9BQQKBgQDQqI+dCxuUvSlptOUIjmDqSTYnXoiFquW/OaEPD/0nDZ6aCqQEhSas8oo+BCyH4nFZwddQ3y3FbCvyuQwRbmbGtDOgUFT9+rdIpZyxo7+84exjt6roZ8P80kZi4JmXFV5kHkMkSgHY12JAGQUy0PkFeRsvbuA+0jRWmxn6h6KBywKBgQDHHuThaHQdmtmolJN8odlmkacw7jTygB/YewyQbDyKj0fegwe8ovlEZScKt3W3lO+bGWjpYKvZHcfqDI1+xWfBWnNqtle2MQ+w3mZVTUmbSE2JsMTiI/keFCTClrZlOgLLz1wapNks9qWZBeZ9mI96d0p1YM05MzuTLUFDHppEEQKBgEmcLY3pbUF7ujBgcKwkDmyYh1I8N01wjLx5SChYK6arV1hgiJxPnOVGax6Vul/1sqdHiv4svaj4EEkAnxFHMVrIF65mqJUw7ZfjVPQWIH4x9YL4OILkmG+IfuxC7+wsza/kPSuAMEafNToHmZ3juvYDU2PYaaO/5FbpQLDxSJMhAoGAGRcZfBWvW/OhLY7bLr8uJ7B+wrmhTlU6rgqDQMeCsWrwgWc9WFWo1ydvzffkC2fm8UI4Z//YZ5lLEYDXa3vwYPrGxFGJp9lywrSjbGg6uChaLNlrVZt2zoV3RFW9UfAHXisYzfqrqvDBWRDXREgu/B1/8l5qPaTpAtiW+qAmjCECgYEAhHb/N3IyVvwjb2KJ9CPk0VglO/M8/j9Rl8/eJcsvQ+AYvE0NIWbhYKQEeCTsK1IQODZtowLUIEMBmV5Z0M9gET0UU71uW9k5IK+JBU9Cz3I0MBxJ9Qzl+/X3nx1JKkNwdY4J5Wmh94fmfqMtN4+BDlJUnVIL7ecpQP9DMEhK9ho=";
    private String NOTIFY_URL = "http://localhost:10086/order/notifyPayResult";
    private String RETURN_URL = "http://localhost:10086/order/goPaySuccPage";
    private String APP_CERT_PATH = root_path + "appCertPublicKey_2016102400751563.crt";
    private String ALIPAY_CERT_PATH = root_path + "alipayCertPublicKey_RSA2.crt";
    private String ALIPAY_ROOT_CERT_PATH = root_path + "alipayRootCert.crt";;
    private String SIGN_TYPE = "RSA2";
    private String CHARSET = "utf-8";
    private String GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do";

    /**
     * 保存加载配置参数
     */
    private static Map<String, String> propertiesMap = new HashMap<>();

    /**
     * 加载属性
     *
     * @PostConstruct，spring的注解，标注表示该方法在容器启动时执行
     */
    @PostConstruct
    public void loadProperties() throws Exception
    {
        // 获得PathMatchingResourcePatternResolver对象
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try
        {
            // 加载resource文件(也可以加载resources)
            Resource resources = resolver.getResource("classpath:alipay.properties");
            PropertiesFactoryBean config = new PropertiesFactoryBean();
            config.setLocation(resources);
            config.afterPropertiesSet();
            Properties prop = config.getObject();
            // 循环遍历所有得键值对并且存入集合
            for (String key : prop.stringPropertyNames())
            {
                propertiesMap.put(key, (String) prop.get(key));
            }
        } catch (Exception e)
        {
            throw new Exception("配置文件加载失败");
        }
    }
}
